package com.example.hszpad.fragment;


import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hszpad.R;
import com.example.hszpad.adapter.BaseRecyclerAdapter;
import com.example.hszpad.adapter.SmartViewHolder;
import com.example.hszpad.bean.MenuItemBean;
import com.example.hszpad.service.ModbusSlaveService;

import java.util.ArrayList;
import java.util.List;

/**
 * 界面控制   平板作为slave端
 */
public class HSZControlFragment_Slave extends Fragment implements View.OnClickListener, ModbusSlaveService.onReceiveListener {
    private static final String TAG = "HSZControlFragment_Slave";

    private RecyclerView recyclerView;
    private BaseRecyclerAdapter<MenuItemBean> recyclerChildAdapter;
    private List<MenuItemBean> menuItemBeanChildList = new ArrayList<>();

    private ModbusSlaveService modbusSlaveService;
    private ServiceConnection serviceConnection;


    private MenuItemBean curItemBean;

    private MenuItemBean curChildBean;

    private ImageView iv_menu01;
    private ImageView iv_menu02;
    private ImageView iv_menu03;
    private ImageView iv_menu04;
    private ImageView iv_menu05;
    private ImageView iv_menu06;
    private ImageView iv_menu07;

    private MenuItemBean bean01 = new MenuItemBean(1, "动力系统", 0, R.drawable.ic_home_white_24dp);
    private MenuItemBean bean02 = new MenuItemBean(2, "电力系统", 0, R.drawable.ic_home_white_24dp);
    private MenuItemBean bean03 = new MenuItemBean(3, "辅助设备", 0, R.drawable.ic_home_white_24dp);
    private MenuItemBean bean04 = new MenuItemBean(6, "破损抗沉管理", 0, R.drawable.ic_home_white_24dp);
    private MenuItemBean bean05 = new MenuItemBean(8, "实时数据", 0, R.drawable.ic_home_white_24dp);
    private MenuItemBean bean06 = new MenuItemBean(9, "历史数据", 0, R.drawable.ic_home_white_24dp);
    private MenuItemBean bean07 = new MenuItemBean(10, "报警列表", 0, R.drawable.ic_home_white_24dp);

    public HSZControlFragment_Slave() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_hszcontrol_slave, container, false);
        findViewById(view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerChildAdapter = new BaseRecyclerAdapter<MenuItemBean>(menuItemBeanChildList, R.layout.layout_first_menu_item, new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MenuItemBean itemBean = menuItemBeanChildList.get(position);
                if (itemBean.isSelect()) {
                    itemBean.setSelect(false);
                    curChildBean = null;
                } else {
                    for (int i = 0; i < menuItemBeanChildList.size(); i++) {
                        menuItemBeanChildList.get(i).setSelect(false);
                    }
                    itemBean.setSelect(true);
                    curChildBean = itemBean;
                    modbusSlaveService.setValue(itemBean.getParent().getAddress(), itemBean.getParent().getId());
                    modbusSlaveService.setValue(itemBean.getAddress(), itemBean.getId());
                }
                recyclerChildAdapter.notifyDataSetChanged();
                recyclerChildAdapter.notifyListDataSetChanged();
            }
        }) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, MenuItemBean model, int position) {
                holder.text(R.id.tv_title, model.getName());
                if (model.isSelect()) {
                    holder.viewBgImageId(R.id.ll_content, R.drawable.ic_second_menu_pressed);
                    holder.textColorId(R.id.tv_title, R.color.colorSecondMenuNormal);
                } else {
                    holder.viewBgImageId(R.id.ll_content, R.drawable.ic_second_menu);
                    holder.textColorId(R.id.tv_title, R.color.colorSecondMenuPressed);
                }
            }
        };
        recyclerView.setAdapter(recyclerChildAdapter);
        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                ModbusSlaveService.LocalBinder binder = (ModbusSlaveService.LocalBinder) service;
                modbusSlaveService = binder.getServices();
                modbusSlaveService.myway();
                modbusSlaveService.start();
                modbusSlaveService.setOnReceiveListener(HSZControlFragment_Slave.this);
                setOnClick(0);
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                modbusSlaveService.setOnReceiveListener(null);
                modbusSlaveService.stop();
                modbusSlaveService = null;
                Log.e(TAG, "onServiceDisconnected:");
            }
        };
        //绑定服务
        Intent intent = new Intent(getActivity(), ModbusSlaveService.class);
        getActivity().bindService(intent, serviceConnection, Service.BIND_AUTO_CREATE);
        return view;
    }

    private void findViewById(View view) {
        iv_menu01 = view.findViewById(R.id.iv_menu01);
        iv_menu02 = view.findViewById(R.id.iv_menu02);
        iv_menu03 = view.findViewById(R.id.iv_menu03);
        iv_menu04 = view.findViewById(R.id.iv_menu04);
        iv_menu05 = view.findViewById(R.id.iv_menu05);
        iv_menu06 = view.findViewById(R.id.iv_menu06);
        iv_menu07 = view.findViewById(R.id.iv_menu07);
        recyclerView = view.findViewById(R.id.recyclerView);
        iv_menu01.setOnClickListener(this);
        iv_menu02.setOnClickListener(this);
        iv_menu03.setOnClickListener(this);
        iv_menu04.setOnClickListener(this);
        iv_menu05.setOnClickListener(this);
        iv_menu06.setOnClickListener(this);
        iv_menu07.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_menu01:
                setOnClick(0);
                break;
            case R.id.iv_menu02:
                setOnClick(1);
                break;
            case R.id.iv_menu03:
                setOnClick(2);
                break;
            case R.id.iv_menu04:
                setOnClick(3);
                break;
            case R.id.iv_menu05:
                setOnClick(4);
                break;
            case R.id.iv_menu06:
                setOnClick(5);
                break;
            case R.id.iv_menu07:
                setOnClick(6);
                break;
        }
    }

    public void setOnClick(int index) {
        iv_menu01.setImageResource(R.drawable.ic_bg_normal);
        iv_menu02.setImageResource(R.drawable.ic_bg_normal);
        iv_menu03.setImageResource(R.drawable.ic_bg_normal);
        iv_menu04.setImageResource(R.drawable.ic_bg_normal);
        iv_menu05.setImageResource(R.drawable.ic_bg_normal);
        iv_menu06.setImageResource(R.drawable.ic_bg_normal);
        iv_menu07.setImageResource(R.drawable.ic_bg_normal);
        switch (index) {
            case 0:
                iv_menu01.setImageResource(R.drawable.ic_bg_pressed);
                curItemBean = bean01;
                break;
            case 1:
                iv_menu02.setImageResource(R.drawable.ic_bg_pressed);
                curItemBean = bean02;
                break;
            case 2:
                iv_menu03.setImageResource(R.drawable.ic_bg_pressed);
                curItemBean = bean03;
                break;
            case 3:
                iv_menu04.setImageResource(R.drawable.ic_bg_pressed);
                curItemBean = bean04;
                break;
            case 4:
                iv_menu05.setImageResource(R.drawable.ic_bg_pressed);
                curItemBean = bean05;
                break;
            case 5:
                iv_menu06.setImageResource(R.drawable.ic_bg_pressed);
                curItemBean = bean06;
                break;
            case 6:
                iv_menu07.setImageResource(R.drawable.ic_bg_pressed);
                curItemBean = bean07;
                break;
        }
        curChildBean = null;
        modbusSlaveService.setValue(curItemBean.getAddress(), curItemBean.getId());
        menuItemBeanChildList.clear();
        menuItemBeanChildList.addAll(getChildMenus(curItemBean));
        recyclerChildAdapter.refresh(menuItemBeanChildList);
        if (!menuItemBeanChildList.isEmpty()) {
            curChildBean = menuItemBeanChildList.get(0);
            modbusSlaveService.setValue(curChildBean.getAddress(), curChildBean.getId());
        }
    }

    public void setOnClickByValue(int value) {
        iv_menu01.setImageResource(R.drawable.ic_bg_normal);
        iv_menu02.setImageResource(R.drawable.ic_bg_normal);
        iv_menu03.setImageResource(R.drawable.ic_bg_normal);
        iv_menu04.setImageResource(R.drawable.ic_bg_normal);
        iv_menu05.setImageResource(R.drawable.ic_bg_normal);
        iv_menu06.setImageResource(R.drawable.ic_bg_normal);
        iv_menu07.setImageResource(R.drawable.ic_bg_normal);
        switch (value) {
            case 1:
                iv_menu01.setImageResource(R.drawable.ic_bg_pressed);
                curItemBean = bean01;
                break;
            case 2:
                iv_menu02.setImageResource(R.drawable.ic_bg_pressed);
                curItemBean = bean02;
                break;
            case 3:
                iv_menu03.setImageResource(R.drawable.ic_bg_pressed);
                curItemBean = bean03;
                break;
            case 6:
                iv_menu04.setImageResource(R.drawable.ic_bg_pressed);
                curItemBean = bean04;
                break;
            case 8:
                iv_menu05.setImageResource(R.drawable.ic_bg_pressed);
                curItemBean = bean05;
                break;
            case 9:
                iv_menu06.setImageResource(R.drawable.ic_bg_pressed);
                curItemBean = bean06;
                break;
            case 10:
                iv_menu07.setImageResource(R.drawable.ic_bg_pressed);
                curItemBean = bean07;
                break;
        }
        modbusSlaveService.setValue(curItemBean.getAddress(), curItemBean.getId());
        menuItemBeanChildList.clear();
        menuItemBeanChildList.addAll(getChildMenus(curItemBean));
        recyclerChildAdapter.refresh(menuItemBeanChildList);
        if (!menuItemBeanChildList.isEmpty()) {
            modbusSlaveService.setValue(menuItemBeanChildList.get(0).getAddress(), menuItemBeanChildList.get(0).getId());
        }
    }


    private List<MenuItemBean> getMenus() {
        List<MenuItemBean> menuItemBeanList = new ArrayList<>();
        MenuItemBean bean = new MenuItemBean(1, "动力系统", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
        bean = new MenuItemBean(2, "电力系统", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
        bean = new MenuItemBean(3, "辅助设备", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
        bean = new MenuItemBean(6, "破损抗沉管理", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
        bean = new MenuItemBean(8, "实时数据", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
        bean = new MenuItemBean(9, "历史数据", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
        bean = new MenuItemBean(10, "报警列表", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
        return menuItemBeanList;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        getActivity().unbindService(serviceConnection);
    }

    private List<MenuItemBean> getChildMenus(MenuItemBean menuItemBean) {
        List<MenuItemBean> menuItemBeanList = new ArrayList<>();
        MenuItemBean bean = null;
        switch (menuItemBean.getId()) {
            case 1:
                bean = new MenuItemBean(menuItemBean, 1, "柴油机系统", 1, R.drawable.ic_home_white_24dp, true);
                menuItemBeanList.add(bean);
                bean = new MenuItemBean(menuItemBean, 2, "轴系/齿轮箱", 1, R.drawable.ic_home_white_24dp, false);
                menuItemBeanList.add(bean);
                break;
            case 2:
                bean = new MenuItemBean(menuItemBean, 1, "电站", 2, R.drawable.ic_home_white_24dp, true);
                menuItemBeanList.add(bean);
                bean = new MenuItemBean(menuItemBean, 2, "配电", 2, R.drawable.ic_home_white_24dp, false);
                menuItemBeanList.add(bean);
                break;
            case 3:
                bean = new MenuItemBean(menuItemBean, 1, "燃油输送", 3, R.drawable.ic_home_white_24dp, true);
                menuItemBeanList.add(bean);
//                bean = new MenuItemBean(menuItemBean, 2, "滑油循环", 3, R.drawable.ic_home_white_24dp, false);
//                menuItemBeanList.add(bean);
                bean = new MenuItemBean(menuItemBean, 3, "冷却系统", 3, R.drawable.ic_home_white_24dp, false);
                menuItemBeanList.add(bean);
                bean = new MenuItemBean(menuItemBean, 4, "电机管理", 3, R.drawable.ic_home_white_24dp, false);
                menuItemBeanList.add(bean);
                bean = new MenuItemBean(menuItemBean, 5, "压载水", 3, R.drawable.ic_home_white_24dp, false);
                menuItemBeanList.add(bean);
                bean = new MenuItemBean(menuItemBean, 6, "生活保障装备", 3, R.drawable.ic_home_white_24dp, false);
                menuItemBeanList.add(bean);
                break;
            case 6:
                bean = new MenuItemBean(menuItemBean, 1, "破损管理", 4, R.drawable.ic_home_white_24dp, true);
                menuItemBeanList.add(bean);
                bean = new MenuItemBean(menuItemBean, 2, "事件回放", 4, R.drawable.ic_home_white_24dp, false);
                menuItemBeanList.add(bean);
                break;
        }
        return menuItemBeanList;
    }

    @Override
    public void onRecevie(int address, int value) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (address == 0) {
                    //第一级
                    if (curItemBean != null) {
                        if (curItemBean.getId() == value) {
                            return;
                        }

                    }
                    setOnClickByValue(value);
                } else {
                    if (value == 0) {
                        return;
                    }
                    if (curChildBean != null) {
                        if (curChildBean.getId() == value) {
                            return;
                        }
                        setSelectChild(value);
                    } else {
                        setSelectChild(value);
                    }
                }
            }
        });
    }

    @Override
    public void onReceiveMultiple(int address, int quantity, int[] values) {
        if (quantity == 1) {
            this.onRecevie(address, values[0]);
        }
    }

    private void setSelectChild(int id) {
        for (int i = 0; i < menuItemBeanChildList.size(); i++) {
            if (menuItemBeanChildList.get(i).getId() == id) {
                menuItemBeanChildList.get(i).setSelect(true);
                curChildBean = menuItemBeanChildList.get(i);
            } else {
                menuItemBeanChildList.get(i).setSelect(false);
            }
        }
        recyclerChildAdapter.notifyDataSetChanged();
        recyclerChildAdapter.notifyListDataSetChanged();
    }
}
